
import es.uned.modelo.Mapa;
import es.uned.controlador.PacmanUtils;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author gabriel
 */
public class TestMapaGrafico extends JPanel {

    private TestMapa mapa;
    private Graphics g;
    private Graphics2D g2d;

    public TestMapaGrafico() {
        setFocusable(true);
        setBackground(Color.BLACK);
        setDoubleBuffered(true);
        this.mapa = new TestMapa();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        this.g = g;
        this.g2d = (Graphics2D) g;

        pintarMapa();
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    private void pintarMapa() {
        if (this.mapa != null) {
            for (int x = 0; x < PacmanUtils.COLUMNAS; x++) {
                for (int y = 0; y < PacmanUtils.FILAS; y++) {
                    pintaBloque(mapa.getElement(x, y),
                            x * PacmanUtils.WIDTH,
                            y * PacmanUtils.HEIGHT,
                            PacmanUtils.WIDTH, PacmanUtils.HEIGHT);
                }
                System.out.println();
            }
        } else {
            System.out.println();
            System.out.println("PacmanPanel.pintarMapa: mapa=null");
        }
    }

    private void pintaBloque(char c, int x, int y, int width, int height) {

        //muro
        if (c == '1') {
            g.setColor(Color.blue);
            g.fillRect(x, y, width, height);
        }
        //power pellets
        if (c == '2') {
            g.setColor(Color.yellow);
            g.fillArc(x + 4, y + 4, width - 14, height - 14, 0, 360);
        }
        //pellets
        if (c == '3') {
            g.setColor(Color.yellow);
            g.fillArc(x + 10, y + 10, width - 25, height - 25, 0, 360);
        }
        //puerta de los fantasmas
        if (c == 'x') {
            g.setColor(Color.GREEN);
            g.fillRect(x, y + 2, width, height - 6);
        }
        //nada
        if (c == '0') {
            g.setColor(Color.black);
            g.fillRect(x, y, width, height);
        }
        //repintamos a pacman si esta marcado en el mapa
//        if (c == 'p') {    
//            pintaAPacman();
//        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        TestMapaGrafico panel = new TestMapaGrafico();
        frame.add(panel);         
        frame.setSize(40*PacmanUtils.COLUMNAS, 40*PacmanUtils.FILAS);
        frame.setLocationRelativeTo(null);
        frame.setTitle("Pacman");   
        frame.setVisible(true);  
    }
    
      


}
